import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chip/bean/Order.dart';
import 'package:flutter_chip/config/Api.dart';
import 'package:flutter_chip/config/Comm.dart';
import 'package:flutter_chip/config/Http.dart';
import 'package:flutter_chip/widget/widget_comm.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

///评论

class CommentPage extends StatefulWidget {
  final userId;

  const CommentPage({Key key, this.userId}) : super(key: key);

  @override
  _CommentPageState createState() => _CommentPageState();
}

class _CommentPageState extends State<CommentPage> {
  //数据集合
  var _item = [];
  EasyRefreshController _easyRefreshController = EasyRefreshController();
  int childCount = 0;

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.custom(
      enableControlFinishRefresh: true,
      header: CommWidget.commHeader(),
      emptyWidget: childCount == 0 ? CommWidget.nullWidget() : null,
      controller: _easyRefreshController,
      enableControlFinishLoad: true,
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
            return Column(
              children: <Widget>[
                _itemWidget(_item[index]),
                Container(height: 8, color: Theme.of(context).selectedRowColor),
              ],
            );
          }, childCount: childCount),
        )
      ],
      onRefresh: () => _getComment(),
    );
  }

  Widget _itemWidget(Order order) {
    return Container(
      color: Theme.of(context).cardColor,
      padding: EdgeInsets.only(left: 10, right: 10, top: 4, bottom: 4),
      child: Column(
        children: <Widget>[
          _userWidget(order),
          _gradeWidget(order),
          _timeWidget(order),
        ],
      ),
    );
  }

  ///用户信息
  Widget _userWidget(Order order) {
    Widget _headWidget = Container(
      width: 28,
      height: 28,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(6),
        child: CachedNetworkImage(
          imageUrl: Api.getUrl(order.buyerImg),
          fit: BoxFit.cover,
          errorWidget: (context, url, error) => Image.asset(
            Comm.IMG_DEFAULT,
            fit: BoxFit.cover,
          ),
        ),
      ),
    );
    return Row(
      children: <Widget>[
        SizedBox(width: 8),
        _headWidget,
        SizedBox(width: 6),
        Text(
          order.buyerName,
          style: TextStyle(
            color: Colors.grey,
            fontWeight: FontWeight.w800,
            fontSize: 12,
          ),
        ),
      ],
    );
  }

  ///评价详情
  Widget _gradeWidget(Order order) {
    return Container(
      padding: EdgeInsets.only(left: 44, top: 10, bottom: 10),
      alignment: Alignment.centerLeft,
      child: Text(
        order.tradeGrade,
      ),
    );
  }

  ///评价时间
  Widget _timeWidget(Order order) {
    return Container(
      padding: EdgeInsets.only(left: 44, bottom: 10),
      alignment: Alignment.centerLeft,
      child: Text(
        order.updateAt,
        style: TextStyle(
          fontSize: 12,
          color: Colors.grey,
        ),
      ),
    );
  }

  ///获取我的评论
  Future _getComment() {
    String url = "${Api.URL_ORDER_COMMENT}?userId=${widget.userId}";
    Http.get(url, successCallBack: (data) {
      List list = json.decode(data).map((m) => Order.fromJson(m)).toList();
      _item.clear();
      _easyRefreshController.finishRefresh();
      setState(() {
        _item.addAll(list);
        childCount = _item.length;
      });
    }, errorCallBack: (error) {
      _easyRefreshController.finishRefresh();
    });
  }
}
